#ifndef Net_TcpServerDispatcher_INCLUDED
#define Net_TcpServerDispatcher_INCLUDED

#include "tcpserverconnection.h"
#include "runnable.h"
#include "thread.h"
#include "threadpool.h"
#include "mutex.h"
#include <map>

using std::map;

class TcpServerDispatcher: public Runnable
{
public:
    TcpServerDispatcher(TcpServerConnectionFactory* pFactory, 
            ThreadPool& threadPool);
    
    virtual void run();

    bool enqueue(int fd);

    void stop();

protected:
    ~TcpServerDispatcher();

private:
    TcpServerDispatcher();
    TcpServerDispatcher(const TcpServerDispatcher&);
    TcpServerDispatcher& operator = (const TcpServerDispatcher&);

private:
    bool                            m_stopped;
    TcpServerConnectionFactory*     m_pConnectionFactory;
    ThreadPool&                     m_threadPool;
    mutable Mutex                   m_mutex;

    map<int,TcpServerConnection*>   m_queue;
    Thread                          m_thread;
};

#endif


